<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/3/2
 * Time: 11:42
 */

namespace App\Http\Logics\MiniProgram;

use App\Http\Logics\CommonLogic;
use App\TrainContent;
use App\Http\Logics\BaseLogic;
class TrainContentLogic extends BaseLogic
{
    public static function updateTrainContent($data)
    {
        // 将之前的删除
        TrainContent::where('train_id' ,$data['train_id'])->delete();

        $content = json_decode($data['content_str']);
        foreach ($content as $key => $item){
            $train_content = new TrainContent();
            // 公众参数
            $train_content->train_id = $data['train_id'];
            $train_content->sort = $key;
            $response = CommonLogic::handleContent($train_content, $item);
            if ($response->code){
                return self::response($response->code, $response->data);
            }
        }
        return self::response(self::SUCCESS);
    }

    public static function getTrainContentDetail($data)
    {
        $train_content = TrainContent::where(function ($query) use ($data){
            $query->where('train_id', $data['train_id']);
        })->orderBy('sort')->get();
        if (empty($train_content)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $train_content);
    }
}